LVS的DR设置

1.环境说明

        三台机器

        director (eth0: 192.168.0.67, vip eth0:0: 192.168.0.64)

        real server1 (eth0 rip:192.168.0.66 ,vip lo:0:192.168.0.64)

        real server2 (eth0 rip:192.168.0.65 ,vip lo:0:192.168.0.64)

2.编写脚本

        director 上执行

1
[root@dir ~]# vim /usr/local/sbin/lvs_dr.sh

        增加内容

1
2
3
4
5
6
7
8
9
10
11
12
#! /bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
ipv=/sbin/ipvsadm
vip=192.168.0.64
rs1=192.168.0.66
rs2=192.168.0.65
ifconfig eth0:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip dev eth0:0
$ipv -C
$ipv -A -t $vip:80 -s rr
$ipv -a -t $vip:80 -r $rs1:80 -g -w 1
$ipv -a -t $vip:80 -r $rs2:80 -g -w 1

        保存后在两台 rs 上执行

1
vim /usr/local/sbin/lvs_dr_rs.sh

        增加内容

1
2
3
4
5
6
7
8
#! /bin/bash
vip=192.168.0.64
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce

        关于 arp_ignore 和 arp_announce 参考:LVS负载均衡中arp_ignore和arp_annonuce参数配置的含义

        然后 driector 上执行

1
[root@dir ~]# sh /usr/local/sbin/lvs_dr.sh

        两台 rs 上执行

1
sh /usr/local/sbin/lvs_dr_rs.sh

3.测试

        windows 下浏览器测试访问 http://192.168.0.64

        更换浏览器

        或者换台计算机用 curl